package com.open.capacity.reimburse.service.impl;

import com.open.capacity.reimburse.entity.StdMealAllowance;
import com.open.capacity.reimburse.util.uuid.UUIDUtils;
import com.open.capacity.utils.DateUtil;
import com.open.capacity.utils.StringUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.open.capacity.commons.PageResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.open.capacity.commons.CodeEnum;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections4.MapUtils;

import com.open.capacity.reimburse.entity.StdTrafficAllowance;
import com.open.capacity.reimburse.dao.StdTrafficAllowanceDao;
import com.open.capacity.reimburse.service.StdTrafficAllowanceService;


@Service
public class StdTrafficAllowanceServiceImpl implements StdTrafficAllowanceService {

    @Autowired
    private StdTrafficAllowanceDao stdTrafficAllowanceDao;

    /**
     * 添加
     *
     * @param stdTrafficAllowance
     */
    public int save(StdTrafficAllowance stdTrafficAllowance) {
        stdTrafficAllowance.setId(UUIDUtils.getOriginalUUIDWithoutBar());
        stdTrafficAllowance.setUpdateTime(new Date());
        return stdTrafficAllowanceDao.save(stdTrafficAllowance);
    }

    /**
     * 修改
     *
     * @param stdTrafficAllowance
     */
    public int update(StdTrafficAllowance stdTrafficAllowance) {
        stdTrafficAllowance.setUpdateTime(new Date());
        return stdTrafficAllowanceDao.update(stdTrafficAllowance);
    }


    /**
     * 删除
     *
     * @param id
     */
    public int delete(String id) {
        return stdTrafficAllowanceDao.delete(id);
    }

    /**
     * 查询记录
     *
     * @param id
     * @return
     */
    public StdTrafficAllowance findById(String id) {
        return stdTrafficAllowanceDao.findById(id);
    }

    /**
     * 分页
     *
     * @param params
     * @return
     */
    public PageResult<StdTrafficAllowance> pageAll(Map<String, Object> params) {
        //设置分页信息，分别是当前页数和每页显示的总记录数【记住：必须在mapper接口中的方法执行之前设置该分页信息】
        if (MapUtils.getInteger(params, "page") != null && MapUtils.getInteger(params, "limit") != null)
            PageHelper.startPage(MapUtils.getInteger(params, "page"), MapUtils.getInteger(params, "limit"), true);

        List<StdTrafficAllowance> list = stdTrafficAllowanceDao.findAll(params);
        PageInfo<StdTrafficAllowance> pageInfo = new PageInfo(list);

        return PageResult.<StdTrafficAllowance>builder().data(pageInfo.getList()).resp_code(CodeEnum.SUCCESS.getCode()).count(pageInfo.getTotal()).build();
    }

    /**
     * 列表
     *
     * @param params
     * @return
     */
    public List<StdTrafficAllowance> findAll(Map<String, Object> params) {
        return stdTrafficAllowanceDao.findAll(params);
    }

    /**
     * 获取市内交通补贴总额
     *
     * @param userId        当事人
     * @param followUserids 随行人员
     * @param destination   目的地
     * @param startDate     开始日期
     * @param endDate       结束日期
     * @return
     */
    @Override
    public BigDecimal getMoney(String userId, String followUserids, Integer persons, String destination, Date startDate, Date endDate) {
        BigDecimal standard = BigDecimal.ZERO;
        String[] destinationArr = destination.split("/");
        String provinceId = destinationArr.length > 0 ? destinationArr[0] : "";
        String cityId = destinationArr.length > 1 ? destinationArr[1] : "";
        String countyId = destinationArr.length > 2 ? destinationArr[2] : "";
        int personCounts = (StringUtil.isEmpty(followUserids) ? 0 : followUserids.split(",").length) + 1;
        //判断手工填写的人数是否比随行人员少，以少的为准
        if (persons < personCounts) {
            personCounts = persons;
        }
        Map<String, Object> params = new HashMap<>();
        List<StdTrafficAllowance> list = stdTrafficAllowanceDao.findAll(params);

        if (CollectionUtils.isNotEmpty(list)) {
            StdTrafficAllowance item = list.get(0);

            int days = DateUtil.getBetweenDay(startDate, endDate);
            if (days <= 0) {
                days = 1;
            }
            standard = item.getMoney().multiply(BigDecimal.valueOf(days)).multiply(BigDecimal.valueOf(personCounts));
        }
        return standard;
    }

    /**
     * 校验市内交通补助是否超标
     *
     * @param userId        当事人
     * @param followUserids 随行人员
     * @param destination   目的地
     * @param startDate     开始日期
     * @param endDate       结束日期
     * @param money         金额
     * @return
     */
    @Override
    public String verify(String userId, String followUserids, Integer persons, String destination, Date startDate, Date endDate, BigDecimal money) {
        String error_msg = "";
        String[] destinationArr = destination.split("/");
        String provinceId = destinationArr.length > 0 ? destinationArr[0] : "";
        String cityId = destinationArr.length > 1 ? destinationArr[1] : "";
        String countyId = destinationArr.length > 2 ? destinationArr[2] : "";
        int personCounts = (StringUtil.isEmpty(followUserids) ? 0 : followUserids.split(",").length) + 1;
        //判断手工填写的人数是否比随行人员少，以少的为准
        if (persons < personCounts) {
            personCounts = persons;
        }
        Map<String, Object> params = new HashMap<>();
        List<StdTrafficAllowance> list = stdTrafficAllowanceDao.findAll(params);

        boolean flag;
        if (CollectionUtils.isNotEmpty(list)) {
            StdTrafficAllowance item = list.get(0);
            BigDecimal standard = BigDecimal.ZERO;

            int days = DateUtil.getBetweenDay(startDate, endDate);
            if (days <= 0) {
                days = 1;
            }
            standard = item.getMoney().multiply(BigDecimal.valueOf(days)).multiply(BigDecimal.valueOf(personCounts));

            flag = money.compareTo(standard) <= 0;
            if (!flag) {
                DecimalFormat decimalFormat = new DecimalFormat("###,###.00");
                error_msg = "<p>⊙ 市内交通补助费用超标, 补贴合计不得超过" + decimalFormat.format(standard) + "元</p>";
            }
        }
        return error_msg;
    }

}
